package edu.tcc.iot.stub.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.tcc.iot.domain.ConnectionInfo;
import edu.tcc.iot.stub.controller.Controller;

public class View extends JFrame {
	
	private static final long serialVersionUID = 1L;
	private Controller controller;
	private JLabel serverIPLb;
	private JTextField serverIPTx;
	private JLabel serverPortLb;
	private JTextField serverPortTx;
	private JLabel shelfIdLb;
	private JTextField shelfIdTx;
	private JButton connectBt;
	private JCheckBox isConnect;
	private JButton sendShelfBt;
	private JButton sendProductsShel1Bt;
	private JButton sendProductsShel2Bt;
	private JPanel connectionPanel;
	private JPanel sendInfoPanel;
	
	public View(Controller controller) {
		this.controller = controller;
		initConnectionUI();
	}

	private final void initConnectionUI(){
		connectionPanel = new JPanel(new GridLayout(1, 2));
		sendInfoPanel = new JPanel(new GridLayout(1, 4));
		JPanel labelPanel = new JPanel(new GridLayout(4, 1));
	    JPanel fieldPanel = new JPanel(new GridLayout(4, 1));
	    connectionPanel.add(labelPanel, BorderLayout.WEST);
	    connectionPanel.add(fieldPanel, BorderLayout.CENTER);
	    
		serverIPLb = new JLabel("Server IP: ");
		serverIPTx = new JTextField();

		serverPortLb = new JLabel("Server Port: ");
		serverPortTx = new JTextField();
		
		shelfIdLb = new JLabel("Shelf ID: ");
		shelfIdTx = new JTextField();
		
		connectBt = new JButton("Connect");
		connectBt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				isConnect.setEnabled(controller.connect());
			}
		});
		
		isConnect = new JCheckBox();
		
		labelPanel.add(serverIPLb);
		fieldPanel.add(serverIPTx);
		labelPanel.add(serverPortLb);
		fieldPanel.add(serverPortTx);
		labelPanel.add(shelfIdLb);
		fieldPanel.add(shelfIdTx);
		labelPanel.add(connectBt);
		fieldPanel.add(isConnect);
		connectionPanel.setSize(1000, 1000);
		
		sendShelfBt = new JButton("Shelf");
		sendProductsShel1Bt = new JButton("Products Shelf 1");
		sendProductsShel2Bt = new JButton("Products Shelf 2");
		
		
		sendShelfBt.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.sendShelf();
			}
		});
		
		sendProductsShel1Bt.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.sendProductsShelf1();
				sendProductsShel1Bt.setEnabled(false);
			}
		});
		
		sendProductsShel2Bt.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.sendProductsShelf2();
				sendProductsShel2Bt.setEnabled(false);
			}
		});
		
		sendInfoPanel.add(sendShelfBt, BorderLayout.EAST);
		sendInfoPanel.add(sendProductsShel1Bt, BorderLayout.CENTER);
		sendInfoPanel.add(sendProductsShel2Bt, BorderLayout.WEST);
		
		getContentPane().add(connectionPanel, BorderLayout.NORTH);
		getContentPane().add(sendInfoPanel, BorderLayout.SOUTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public ConnectionInfo getConnectionInfo() {
		ConnectionInfo info = new ConnectionInfo();
		info.setServerIP(serverIPTx.getText());
		info.setServerPort(Integer.parseInt(serverPortTx.getText()));
		info.setShelfID(Long.parseLong(shelfIdTx.getText()));
		return info;
	}

}
